package com.nameless.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nameless.entity.Coupon;
import com.nameless.entity.CouponHistory;
import com.nameless.entity.Shop;
import com.nameless.service.CouponService;
import com.nameless.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * -*- coding:utf-8 -*-
 *
 * @author 太阳偷渡青山
 * @software IntelliJ IDEA
 * @date 2023/02/27 9:49
 * @Description
 */
@RestController
@Api("优惠劵api信息操作接口")
@Slf4j
@RequestMapping("/coupon/api")
public class ApiCouponController {

    @Autowired
    private CouponService couponService;

    @PostMapping("{current}/{pageSize}")
    @ApiOperation(value = "分页查询信息并携带查询条件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页码，默认为1",example = "1",
                    paramType = "path",dataType = "int",required = false),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数,默认5", example = "5",
                    paramType = "path", dataType = "int", required = false)
    })
    public Result<Page<Coupon>> findPageCustomer(@PathVariable Integer current, @PathVariable Integer pageSize,
                                                 @RequestBody(required = false) Coupon searchObj){


        // 构造一个分页器，传递当前页与每页的页数
        Page<Coupon> setPage = new Page<>(current, pageSize);

        LambdaQueryWrapper<Coupon> wrapper = new LambdaQueryWrapper<>();

        wrapper.like(StringUtils.isNotEmpty(searchObj.getName()),Coupon::getName,searchObj.getName());

        Page<Coupon> page = couponService.page(setPage, wrapper);
        return Result.success(page);
    }

    @GetMapping("{id}")
    @ApiOperation(value = "根据优惠劵编号信息")
    public Result<Coupon> getOne(@PathVariable Long id){
        Coupon byId = couponService.getById(id);
        return Result.success("id为："+id+"的优惠劵查询成功",byId);
    }

    @GetMapping("findAll")
    @ApiOperation(value = "查询所有优惠劵")
    public Result<List<Coupon>> getList(){
        List<Coupon> couponHistoryList = couponService.list();
        return Result.success(couponHistoryList);
    }

    @GetMapping("/shop/{shopId}")
    @ApiOperation(value = "根据门店id查询信息")
    public Result<List<Coupon>> getOne( @PathVariable Integer shopId){
        LambdaQueryWrapper<Coupon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Coupon::getShopId,shopId);
        List<Coupon> couponHistoryList = couponService.list(queryWrapper);
        return Result.success("门店id为："+shopId+"的优惠劵查询成功",couponHistoryList);
    }

    @DeleteMapping
    @ApiOperation(value = "删除多个优惠劵")
    @ApiImplicitParam(name = "idList",value = "需要删除的医院配置id集合")
    public Result<String> delete(@RequestBody List<Integer> idList){
        log.info("idList = {}",idList);
        boolean b = couponService.removeByIds(idList);
        return b?Result.success("删除成功"):Result.error("删除失败");
    }

    @PostMapping
    @ApiOperation(value = "添加优惠劵信息")
    public Result<String> addCustomer(@RequestBody Coupon coupon){
        log.info("{}" , coupon);
        if (coupon.getValue() != null){
            coupon.setUnitDesc("元");
            coupon.setValueDesc(coupon.getValue().toString());
        }
        boolean save = couponService.save(coupon);
        return save?Result.success("添加成功"):Result.error("添加失败");
    }

    @PutMapping
    @ApiOperation(value = "修改优惠劵信息")
    public Result<String> editCustomer(@RequestBody Coupon coupon){
        boolean save = couponService.updateById(coupon);
        return save?Result.success("修改成功"):Result.error("修改失败");
    }


    @GetMapping("shopAll")
    public Result<List<Shop>> selectAllOtherInfo(){
        return Result.success("查询门店信息成功",couponService.getOtherInfo());
    }

    /**
     * 选择所有其他信息通过商店id
     *
     * @param shopId 商店id
     * @return {@link Result}<{@link Map}<{@link String},{@link Object}>>
     */
    @GetMapping("shopAll/{shopId}")
    public Result<Map<String,Object>> selectAllOtherInfoByShopId(@PathVariable Integer shopId){
        return Result.success("查询门店信息成功",couponService.getOtherInfoByShopId(shopId));
    }
}
